package com.phychips.rcp;

import android.bluetooth.BluetoothAdapter;
import com.phychips.bluetooth.BluetoothTransceiver;

//import com.phychips.bluetooth.BluetoothTransceiver;
import com.phychips.common.OnBytesAvailableListener;
import com.phychips.common.ReaderIo;
import com.phychips.common.ReaderIoConst;


public class BluetoothIo extends ReaderIo
{
    // Tagon Device ==> App
    final public static byte CMD_CONNECT = (byte) 0xe9;
    final public static byte CMD_DISCONNECT = (byte) 0xea;
    final public static byte CMD_TIMEOUT = (byte) 0xeb;
    final public static byte CMD_BATTERY_LOW = (byte) 0xec;
    final public static byte CMD_UART_ERR = (byte) 0xed;

    private static iBluetoothEvent mBluetoothEvent = null;
    private static BluetoothAdapter m_Adapter;
    private static BluetoothTransceiver mBluetoothTransceiver = null;

    static private boolean opened = false;

    public void setBluetoothEvent(iBluetoothEvent e)
    {
	mBluetoothEvent = e;
	getBluetoothApi().setBluetoothEvent(mBluetoothEvent);
    }

    synchronized public BluetoothTransceiver getBluetoothApi()
    {
	try
	{
	    if (mBluetoothTransceiver == null)
	    {
		if (m_Adapter == null)
		    m_Adapter = BluetoothAdapter.getDefaultAdapter();

		mBluetoothTransceiver = new BluetoothTransceiver(m_Adapter);
	    }
	}
	catch (Exception e)
	{
	    e.printStackTrace();
	}

	return mBluetoothTransceiver;
    }

    @Override
    public void setOnBytesAvailableListener(OnBytesAvailableListener listener)
    {
	getBluetoothApi().registerBytesAvailableListener(listener);
    }

    @Override
    synchronized public void open() throws RcpException
    {
	String destAddr = null;

	if (opened == true)
	{
	    RcpException exception = new RcpException(
		    "Fail: already opened", ReaderIoConst.SERIAL_OPEN_FAIL);
	    throw exception;
	}

	if (m_Adapter == null)
	    m_Adapter = BluetoothAdapter.getDefaultAdapter();

	if (m_Adapter == null)
	{
	    RcpException exception = new RcpException(
		    "Fail: Bluetooth is not available.",
		    ReaderIoConst.BLUETOOTH_NOT_AVAILABLE);

	    throw exception;
	}

	if (!m_Adapter.isEnabled())
	{
	    RcpException exception = new RcpException(
		    "Fail: Bluetooth is disabled.",
		    ReaderIoConst.BLUETOOTH_NOT_ENABLE);

	    throw exception;
	}

	if (mBluetoothTransceiver == null)
	{
	    mBluetoothTransceiver = new BluetoothTransceiver(m_Adapter);
	}

	if (destAddr == null)
	{
	    mBluetoothTransceiver.startDiscovery();
	}
    }

    @Override
    public void close() throws RcpException
    {
	if (mBluetoothTransceiver != null)
	{
	    mBluetoothTransceiver.stopDiscovery();
	    mBluetoothTransceiver.disconnect();
	}
    }

    @Override
    public void TransmitData(byte[] data, int length) throws RcpException
    {
	byte[] temp = new byte[length];
	System.arraycopy(data, 0, temp, 0, temp.length);

	if(data[2] == 0x27 || data[2] == 0x22)
	{
	    mBluetoothTransceiver.AutoreadStart();
	}
	else if(data[2] == 0x28)
	{
	    mBluetoothTransceiver.AutoreadStop();
	    return;
	}
	
	mBluetoothTransceiver.write(temp);
	
	System.out.println();	
	for (int i = 0; i < temp.length; i++)
	{
	    System.out.printf("%02X ", (byte) temp[i]);
	}
	System.out.println();
	
	log_write(logType.CMD, data);
    }

}
